package com.wyz.saas.common.notifice.intent

import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import com.wyz.saas.common.notifice.AtomDataRegistry
import com.wyz.saas.common.notifice.receiver.AtomBroadcastReceiver

class ReceiverIntent<T : Any> : Intenter {

    companion object {
        const val NAME = "ReceiverIntent"
    }

    private val receiver: Class<out AtomBroadcastReceiver<T>>
    private val ctx: Context
    private val data: T

    constructor(
        ctx: Context,
        receiver: Class<out AtomBroadcastReceiver<T>>,
        data: T
    ) {
        this.receiver = receiver
        this.ctx = ctx
        this.data = data
    }

    override fun toPending(): PendingIntent {
        val intent = Intent(ctx, receiver)
        intent.action = receiver.getConstructor().newInstance().getActionID()
        intent.putExtra(NAME, AtomDataRegistry.register(data))
        return PendingIntent.getBroadcast(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
    }
}